//
//  JYPresentationController.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/2.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit

class JYPresentationController: UIPresentationController {
    // MARK:- 对外提供属性
    var presentedFrame : CGRect = CGRectZero
    
    
    // MARK:- 懒加载属性
    private lazy var coverView : UIView = UIView()
    
    // MARK:- 系统回调函数
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        //设置弹出控制器尺寸
        presentedView()?.frame = presentedFrame
        
        //添加萌版
        setupCoverView()
    }
}

// MARK:- 设置UI界面
extension JYPresentationController {
    private func setupCoverView() {
        //添加蒙板
        containerView?.insertSubview(coverView, atIndex: 0)
        
        //设置蒙板属性
        coverView.frame = containerView!.bounds
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.3)
        
        //添加点击手势
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(JYPresentationController.coverViewClick))
        coverView .addGestureRecognizer(tapGes)
    }
}

// MARK:- 事件监听
extension JYPresentationController {
    @objc private func coverViewClick() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
}
